#include "nrmare.h"
#include <fstream>
#include <iostream>
#define LG_MAX 500+1
#include <string.h>
int s[500];
using std::cin;
using std::cout;
void NrMare::citire(){
	  int i;
    char s[LG_MAX];
    cin>>s;                                   
    x[0]=strlen(s);                               
    for(i=x[0]-1;i>=0;i--)
			 x[x[0]-i]=s[i]-'0';
    for(i=x[0]+1;i<LG_MAX;i++) 
			 x[i]=0;
}
void NrMare::afisare(){

     for(int i=x[0];i>=1;i--)
        cout<<x[i];
}

NrMare NrMare::operator +(NrMare& right){
    int i,cifra,t=0,max;
		NrMare s;
    if(x[0]<right.x[0])
		 { 
				max=right.x[0];
				for(i=x[0]+1;i<=right.x[0];i++)
					 x[i]=0;
 		 }
    else { 
				max=x[0];
			  for(i=right.x[0]+1;i<=x[0];i++)
			 		right.x[i]=0;
		 }
 			
    for(i=1;i<=max;i++)
    {
      cifra=x[i]+right.x[i]+t;
      s.x[i]=cifra%10;     
      t=cifra/10;           
    }
    if(t)
		 s.x[i]=t; 
		else i--;
    s.x[0]=i;
		return s;
}
NrMare NrMare::operator -(NrMare& right){
		int i,t = 0;
	  NrMare d;
    if(x[0] < right.x[0])
     {
				 d=*this-right;
     }
		if(x[0]>right.x[0])
			 d=*this-right;
    else
     {
       for(i = 1; i <= x[0]; i++)
       {         
           d.x[i] = x[i]-right.x[i]+t;        
           if(d.x[i] < 0) d.x[i]+= 10,t = -1; 
           else t = 0;
        }
        i--;
        while(i && !d.x[i]) i--;
        d.x[0] = i;
		} 
		return d;
}
NrMare NrMare::operator *(NrMare& right){
    int i,j,t,cifra;
		NrMare p;
    for(i = 0; i < LG_MAX; i++) p.x[i] = 0;
     
    for(i = 1; i <= right.x[0]; i++)
     {
       for(t=0,j=1; j <= x[0]; j++)
        {
          cifra = p.x[i+j] + x[j] * right.x[i] + t; 
				  p.x[i+j] = cifra % 10;
					t = cifra / 10;
				                                           
        }   
       if(t) p.x[i+j] = t;
     }
		p.x[0] = x[0] + right.x[0];
    if(p.x[0]) p.x[0]++;
		return p;
}
bool NrMare::operator ==(NrMare& right){
	int i;
	if(x[0]!=right.x[0])
			return false;
	else
    for(i = x[0]; i >= 1 && x[i] == right.x[i]; i--)
      ;
    if(i <= 0) return true;
	return false;
}
bool NrMare::operator <(NrMare& right){
	    int i;
    if(x[0] > right.x[0])
				return false;
		else
    for(i = x[0]; i >= 1 && x[i] == right.x[i]; i--)
      ;
    if(x[i] < right.x[i])
				return true;
    return false;
}
bool NrMare::operator >(NrMare& right){
    int i;
    if(x[0] < right.x[0])
				return false;
		else
    for(i = x[0]; i >= 1 && x[i] == right.x[i]; i--)
      ;
    if(x[i] < right.x[i])
			 return false;
    return true;
}
void NrMare::scrieFisier(){
		
		std::ofstream out("scrie.txt");
		for(int i=x[0];i>=1;i--)
		{
        out<<x[i];				
		}		
		out<<"\n";
}


SirNrMari::SirNrMari(NrMare& unu){
	i=-1;
	v[++i]=unu;
}
void SirNrMari::add(NrMare& unu){
	v[++i]=unu;
}
void SirNrMari::afisare(){
	for(int j=0;j<=i;j++)
		{	
		for(int i=v[j].x[0];i>=1;i--)
       cout<<v[j].x[i];
		}
}
void SirNrMari::max(){
	NrMare max;	
	max=v[0];
	for(int j=1;j<=i;j++)
			{	
				if(max<v[j])
					max=v[j];
			}
	max.afisare();	
}
void SirNrMari::ordonare(){
	  int ii, j;
		NrMare aux; 
    for (ii = 1; ii <= i; ii++)
     { 
       j = ii; 
       while (j > 0 && v[j]<v[j - 1] )
        { 
            aux = v[j]; 
            v[j] = v[j - 1]; 
            v[j - 1] = aux; 
            j--; 
        } 
     }
}
void SirNrMari::eliminareSuma(){
		NrMare suma;
		NrMare ls,ld;
		//ls=-32769,ld=32768
		for(int j=0;j<=i;j++)
			{	
				if(ls<v[j]&&v[j]<ld)
					{
							suma=suma+v[j];
							v[j]=v[j+1];
							j--;
					}
			}
}
bool SirNrMari::distincte(){
	int x=1;		
	for(int j=0;j<i;j++)
		{	
      if((v[j]==v[j+1]))
					x=0;
		}
	if (x==0)
			return false;
	else
		return true;

}
